package com.zjp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjp.pojo.Area;
import com.zjp.pojo.City;
import com.zjp.pojo.DetectionPoint;
import com.zjp.pojo.Province;
import com.zjp.service.AreaService;
import com.zjp.service.DetectionPointService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 检测管理
 */
@RestController
@Slf4j
public class DetectionPointController {
    @Autowired
    private DetectionPointService detectionPointService;
    @Autowired
    private AreaService areaService;

    /**
     * 分页查询核酸检测点
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/findAlldetectionPoint")
    public ModelAndView findAlldetectionPoint(@RequestParam(name = "pageNo", required = false, defaultValue = "1") int pageNo,
                                              @RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize) {
        //创建一个ModelAndView对象
        ModelAndView mv = new ModelAndView();
        //开启分页，设置当前页和每页显示的记录数
        PageHelper.startPage(pageNo, pageSize);   //开启分页
        //查询所有检测点
        List<DetectionPoint> detectionPointList = detectionPointService.findAllDetectionPoint();
        //打印检测点列表
        log.info("检测点列表：{}", detectionPointList);
        //将检测点列表添加到ModelAndView中
        mv.addObject("detectionPointList", detectionPointList);
        //创建一个PageInfo对象，存储分页信息
        PageInfo pageInfo = new PageInfo<>(detectionPointList);  //存储分页信息
        //将分页信息添加到ModelAndView中
        mv.addObject("pageInfo", pageInfo);
        //查询所有省份
        List<Province> provinceList = areaService.findAllProvince();
        //将省份列表添加到ModelAndView中
        mv.addObject("provinceList", provinceList);
        //查询所有城市
        List<City> cityList = areaService.findAllCity();
        //将城市列表添加到ModelAndView中
        mv.addObject("cityList", cityList);
        //查询所有地区
        List<Area> areaList = areaService.findAllArea();
        //将地区列表添加到ModelAndView中
        mv.addObject("areaList", areaList);
        //设置视图名称
        mv.setViewName("detecionpoint");
        //返回ModelAndView
        return mv;
    }

    /**
     * 添加检查点
     */
    @RequestMapping("/addDetectionPoint")
    public String addDetectionPoint(DetectionPoint detectionPoint) {
        int i = detectionPointService.addDetectionPoint(detectionPoint);
        log.info("添加检查点结果：{}", i);
        if (i > 0) {
            return "SUCCESS";
        }
        return "error";
    }

    /**
     * 修改检查点
     */
    @RequestMapping("/updateDPoint")
    public String updateDPoint(DetectionPoint detectionPoint) {
        int i = detectionPointService.updateDPoint(detectionPoint);
        log.info("修改检查点结果：{}", i);
        if (i > 0) {
            return "SUCCESS";
        }
        return "error";
    }

    /**
     * 删除检查点
     */
    @RequestMapping("/deleteDPointById")
    public String deleteDPointById(Integer detectionid){
        int i=detectionPointService.deleteDPointById(detectionid);
        log.info("删除检查点结果：{}",i);
        if(i>0){
            return "SUSSES";
        }
        return "error";
    }
}


